var app=new Vue({
    el: '#app',
    data:{
        dictList: null,
        dictTree: null,
        dictName: '',
        searchDict:{
        }
    },
    watch: {
        dictName: function(val) {
            this.$refs.dictTree.filter(val);
        }
    },
    methods:{
        init: function(){
            var _this=this;
            setTimeout(function(){
                _this.getDictTree();
            },100);
        },
        /**
         * 过滤字典
         */
        filterName:function(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        getDictTree: function(){
            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/dict/getList",
                data:{leLevel:2}
            }).done(function(res){
                _this.dictTree=jsmile.tree.toDictTree(res.resData,{textField:"dictName"});
            }).fail(function(res){
                jsmile.msg.error(res.resMsg?res.resMsg:"获取数据字典树失败!");
            });
        },
        addDict: function(){
            var _this=this;
            var node=this.$refs.dictTree.getCurrentNode();
            if(!node){
                top.app.$alert('请选择左侧的字典!', '提示',{type: 'warning'});
                return false;
            }
            if('0'==node.addable){
                top.app.$alert('该字典类型不允许新增!', '提示',{type: 'warning'});
                return false;
            }
            top.layer.open(jsmile.dialog.init({
                title: "新增字典项",
                content: "sys/dict/add.html",
                success: function(layero,index){
                    var iframeW=layero.find('iframe')[0];
                    var dict={
                        addable:1,
                        visible: 1,
                        parentId: node.id,
                        level: node.level+1
                    };
                    iframeW.contentWindow.app.dict=dict;
                }
            }));
        },
        editDict: function(index,row){
            top.layer.open(jsmile.dialog.init({
                title: "修改字典项",
                content: "sys/dict/edit.html",
                success: function(layero,index){
                    var iframeW=layero.find('iframe')[0];
                    iframeW.contentWindow.app.dict=row;
                }
            }));
        },
        deleteDict: function(index,row){
            var _this=this;
            top.app.$confirm('此操作将永久删除该字典项, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function(){
                jsmile.http.sendJson({
                    url:jsmile.url+"/sys/dict/delete",
                    data:{"id":row.id}
                }).done(function(res){
                    jsmile.msg.success("删除字典成功");
                    _this.findAll();
                }).fail(function(res){
                    jsmile.msg.error(res.resMsg?res.resMsg:"删除字典失败!");
                });
            }).catch(function(){
                jsmile.msg.info( '已取消删除');
            });
        },
        goHome: function(url){
            top.$('#home').attr("src","homePage/home.html");
        },
        handleSizeChange: function(val) {
            this.searchDict.pageSize=val;
            this.findAll();
        },
        handleCurrentChange: function(val) {
            this.searchDict.pageIndex=val;
            this.findAll();
        },
        handleNodeClick: function(data,node,event) {
            if(2==data.level){
                this.searchDict={
                    parentId: data.id
                }
            }else{
                this.searchDict={
                    parentId: -1
                }
            }
            this.findAll();
        },
        /*
         * 获取所有数据
         */
        findAll: function(){
            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/dict/findAll",
                data:_this.searchDict
            }).done(function(res){
                _this.dictList=res.resData.list;
            }).fail(function(res){
                jsmile.msg.error(res.resMsg?res.resMsg:"获取字典列表失败!");
            });
        }
    }
});
$(function(){
    init();
    function init(){
        app.init();
    }
});